package models

import (
	"database/sql"
	"strings"
)

type TableInfo struct {
	TableName    string
	TableType    string
	Engine       string
	TableComment string
}

func GetTableInfos() (table_infos []TableInfo, err error) {
	rows, err := mainDB.Query("SELECT A.name,C.value FROM sys.tables A inner JOIN sys.extended_properties C ON C.major_id = A.object_id  and minor_id=0")
	defer rows.Close()
	if err != nil {
		return table_infos, err
	}
	return _TableInfoRowsToArray(rows)
}

func _TableInfoRowsToArray(rows *sql.Rows) (models []TableInfo, err error) {
	for rows.Next() {
		model := TableInfo{}
		err = rows.Scan(&model.TableName, &model.TableComment)
		if err != nil {
			return models, err
		}
		if len(model.TableComment) > 0 {
			model.TableComment = strings.Replace(model.TableComment, "\r", "", -1)
			model.TableComment = strings.Replace(model.TableComment, "\n", "", -1)
		}
		models = append(models, model)
	}
	return models, err
}
